package org.example.Backtracking_algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

public class Full_arrangement_II {
    public static List<List<Integer>> result = new ArrayList<>();
    public static LinkedList<Integer> value = new LinkedList<>();

    public static void main(String[] args) {
        //全排列 II

        //给定一个可包含重复数字的序列 nums ，按任意顺序 返回所有不重复的全排列。
        int[] nums = {3,3,0,3};
        permuteUnique(nums);
        prin();
    }
    public static List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        List<Integer> arrayList = Arrays.stream(nums).boxed().collect(Collectors.toList());
        get(arrayList,nums.length);
        return result;
    }
    public static void get(List<Integer> arrayList,int len){
        if (value.size()==len || arrayList.size()==0){
            result.add(new ArrayList<>(value));
            return;
        }
        for (int i = 0; i < arrayList.size(); i++) {
            if (i>0 && arrayList.get(i-1)==arrayList.get(i)) continue;
            ArrayList<Integer> list = new ArrayList<>(arrayList);
            value.add(list.get(i));
            list.remove(i);
            get(list,len);
            value.removeLast();
        }
    }

    public static void prin() {
        for (List<Integer> list : result) {
            System.out.print("[ ");
            for (Integer integer : list) {
                System.out.print(integer + " ");
            }
            System.out.print(" ]");
        }
        System.out.println();
    }
}
